package cn.quicklydoit.service.impl;

import cn.quicklydoit.entity.CacheData;
import cn.quicklydoit.entity.User;
import cn.quicklydoit.mapper.UserMapper;
import cn.quicklydoit.service.UserService;
import cn.quicklydoit.utils.CommonUtils;
import cn.quicklydoit.utils.TokenUtils;
import cn.quicklydoit.utils.UserNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.transaction.Transactional;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.HashMap;
import java.util.Map;

@Service
@Transactional(rollbackOn = Exception.class)
@ApplicationScoped
public class UserImpl implements UserService {

    @Inject
    UserMapper userMapper;

    private static Map<String, CacheData> cacheEmailValues=new HashMap<>();


    @Override
    public void createUser(User user){
        this.userMapper.save(user);
    }

    @Override
    public int insert(User user) {
        User save = this.userMapper.save(user);
        return save==null?0:1;
    }

    @Override
    public Iterable<User> getAll() {
        return this.userMapper.findAll();
    }

    @Override
    public Response login(User user) {
        //TODO 用户登录
        //验证用户是否正确登录
        //user.getPassword()使用md5 解密
        User login = this.userMapper.login(user.getName(), user.getPassword());

        if(login==null){
            throw new UserNotFoundException("");
        }
        //添加token
        login.setToken(TokenUtils.GenerateToken(user));

        login.setCurrentTimes(user.getCurrentTimes()+1);

        login.setCurrentIp(user.getCurrentIp());

        this.userMapper.save(login);

        return Response.status(Response.Status.OK).entity(login).type(MediaType.APPLICATION_JSON).build();
    }

    @Override
    public Response sendValidValues(User user,String methodName) {
        //验证用户是否存在
        String name = user.getName();
        String email= user.getEmail();
        if(StringUtils.isEmpty(name)){
            return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE).tag("用户名为空").build();
        }else if(StringUtils.isEmpty(email)){
            return  Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE).tag("邮箱为空").build();
        }else{
            //验证邮箱是否符合规则
            if(!CommonUtils.isEmail(email)){
                return  Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE).tag("邮箱不正确").build();
            }
            CacheData cacheData= new CacheData();

            cacheData.setKey(user.getName()+"~"+methodName);

            cacheData.setValue("");



            return  Response.status(Response.Status.OK).entity(user).build();
        }


    }

    @Override
    public Response register(User user) {
        //判断用户是否存在

        CacheData cacheData = cacheEmailValues.get(user.getName());
        //判断是否超时

        if(cacheData.isExpire()){
            return Response.status(Response.Status.REQUEST_TIMEOUT).tag("验证码超时").build();
        }

        if(user.getValidCode()==null || user.getValidCode().equals(cacheData.getValue())){
            return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE).tag("验证码输入错误").build();
        }
        this.userMapper.save(user);

        return Response.status(Response.Status.OK).entity(user).build();
    }
}
